<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>初始化一个 readline 回调接口，然后终端输出提示信息并立即返回</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.readline-add-history.html">« readline_add_history</a></li>
      <li style="float: right;"><a href="function.readline-callback-handler-remove.html">readline_callback_handler_remove »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.readline.html">Readline 函数</a></li>
    <li>初始化一个 readline 回调接口，然后终端输出提示信息并立即返回</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.readline-callback-handler-install" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">readline_callback_handler_install</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.1.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">readline_callback_handler_install</span> &mdash; <span class="dc-title">初始化一个 readline 回调接口，然后终端输出提示信息并立即返回</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.readline-callback-handler-install-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>readline_callback_handler_install</strong></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$prompt</code></span>, <span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> <code class="parameter">$callback</code></span>): <span class="type">bool</span></div>

  <p class="para rdfs-comment">
   设置一个 readline 回调接口然后输出 <code class="parameter">prompt</code> 并立即返回.
   第二次调用这个函数不需要移除上一个回调接口，这个函数将自动覆盖旧的接口.
  </p>
  <p class="para">
   当配合 <span class="function"><a href="function.stream-select.html" class="function">stream_select()</a></span> 时回调的特性非常有用，它允许在 IO 与用户输入
   间交叉进行，不像<span class="function"><a href="function.readline.html" class="function">readline()</a></span>.
  </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-function.readline-callback-handler-install-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">prompt</code></dt>

     <dd>

      <p class="para">
       提示信息.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">callback</code></dt>

     <dd>

      <p class="para">
       <code class="parameter">callback</code> 函数需要一个参数; 用户输入将被返回.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.readline-callback-handler-install-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时返回 <strong><code>true</code></strong>， 或者在失败时返回 <strong><code>false</code></strong>。
  </p>
 </div>

 
 <div class="refsect1 description" id="refsect1-function.readline-callback-handler-install-description">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-701">
    <p><strong>示例 #1 Readline Callback Interface Example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">rl_callback</span><span style="color: #007700">(</span><span style="color: #0000BB">$ret</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;global&nbsp;</span><span style="color: #0000BB">$c</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$prompting</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"You&nbsp;entered:&nbsp;</span><span style="color: #0000BB">$ret</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$c</span><span style="color: #007700">++;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">$c&nbsp;</span><span style="color: #007700">&gt;&nbsp;</span><span style="color: #0000BB">10</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$prompting&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">readline_callback_handler_remove</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">readline_callback_handler_install</span><span style="color: #007700">(</span><span style="color: #DD0000">"[</span><span style="color: #0000BB">$c</span><span style="color: #DD0000">]&nbsp;Enter&nbsp;something:&nbsp;"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'rl_callback'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">$c&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$prompting&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">readline_callback_handler_install</span><span style="color: #007700">(</span><span style="color: #DD0000">"[</span><span style="color: #0000BB">$c</span><span style="color: #DD0000">]&nbsp;Enter&nbsp;something:&nbsp;"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'rl_callback'</span><span style="color: #007700">);<br /><br />while&nbsp;(</span><span style="color: #0000BB">$prompting</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$w&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">NULL</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$e&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">NULL</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$n&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">stream_select</span><span style="color: #007700">(</span><span style="color: #0000BB">$r&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #0000BB">STDIN</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">$w</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$e</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">null</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">$n&nbsp;</span><span style="color: #007700">&amp;&amp;&nbsp;</span><span style="color: #0000BB">in_array</span><span style="color: #007700">(</span><span style="color: #0000BB">STDIN</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$r</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;read&nbsp;a&nbsp;character,&nbsp;will&nbsp;call&nbsp;the&nbsp;callback&nbsp;when&nbsp;a&nbsp;newline&nbsp;is&nbsp;entered<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">readline_callback_read_char</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br />echo&nbsp;</span><span style="color: #DD0000">"Prompting&nbsp;disabled.&nbsp;All&nbsp;done.\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.readline-callback-handler-install-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.readline-callback-handler-remove.html" class="function" rel="rdfs-seeAlso">readline_callback_handler_remove()</a> - 移除上一个安装的回调函数句柄并且恢复终端设置</span></li>
    <li class="member"><span class="function"><a href="function.readline-callback-read-char.html" class="function" rel="rdfs-seeAlso">readline_callback_read_char()</a> - 当一个行被接收时读取一个字符并且通知 readline 调用回调函数</span></li>
    <li class="member"><span class="function"><a href="function.stream-select.html" class="function" rel="rdfs-seeAlso">stream_select()</a> - Runs the equivalent of the select() system call on the given
   arrays of streams with a timeout specified by seconds and microseconds</span></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>